package family

import (
	"fmt"
)

// 登录菜单
func (f *Family) loginMenu() {
	name := f.users[f.uid].name
	fmt.Printf("----------------------您好，%s------------------------\n", name)
	fmt.Println("------------------------1登记收入------------------------------")
	fmt.Println("------------------------2登记支出------------------------------")
	fmt.Println("------------------------3收支明细------------------------------")
	fmt.Println("------------------------4转账--------------------------------")
	fmt.Println("------------------------5修改密码----------------------------")
	fmt.Println("------------------------6注销--------------------------------")
	fmt.Printf("------------------------请选择(1-6):")
}

// 修改密码！
func (f *Family) changePWD() {
	var pwd string
	fmt.Printf("------------------------请输入新密码:")
	fmt.Scanln(&pwd)
	if pwd == f.users[f.uid].pwd {
		fmt.Println("--------------------您未作任何修改！")
	} else {
		f.users[f.uid].pwd = pwd
		fmt.Println("------------------------修改成功！")
	}
}

// 登录
func (f *Family) login() bool {
	var name, pwd string
	fmt.Printf("------------------------请输入用户名:")
	fmt.Scanln(&name)
	fmt.Printf("------------------------请输入密码:")
	fmt.Scanln(&pwd)
	for id, val := range f.users {
		if val.name == name && val.pwd == pwd {
			f.uid = id
			fmt.Println("\t\t登录成功！")
			return true
		}
	}
	fmt.Println("\t\t用户名或密码错误！")
	return false
}

// 注册
func (f *Family) register() {
	loop := true
	u := user{balance: 0, records: make([]record, 0)}
	for loop {
		loop = false
		fmt.Printf("------------------------请输入用户名:")
		fmt.Scanln(&u.name)
		fmt.Printf("------------------------请输入密码:")
		fmt.Scanln(&u.pwd)
		for _, val := range f.users {
			if val.name == u.name {
				fmt.Println("\t该用户已存在！请重新输入！")
				loop = true
				break
			}
		}
	}
	f.users = append(f.users, u)
	f.uid = len(f.users) - 1
	fmt.Println("\t\t注册成功！已为您自动登录。")
}
